<?php
require_once('wpfunctions.php');
function filterOnTerm($v){
  global $termArray;
  $v = mb_strtolower($v,'UTF-8');
  foreach($termArray as $t) {
    if($t && strpos($v, $t)===false) {
      return false;
    }
  }
  return true;
}
header("Content-type: application/json");
$root = $_REQUEST['root'];
$extension = $_REQUEST['extension'];
$hideExtension = $_RQUEST['hideExtension'];
$term = $_REQUEST['term'];
$termArray = explode(' ',mb_strtolower($term,'UTF-8'));
if($root == 'liturgy') {
  require_once('liturgyfunctions.php');
  $table = getLiturgyTable();
  $results = Array();
  $title = $_REQUEST['title'];
  if(!$title) {
    foreach($table as $i => $row) {
      $results[] = $row['Title'];
    }
  } else {
    foreach($table as $i => $row) {
      if($row['Title'] == $title) {
        $results[] = $row[$extension];
      }
    }
  }
  echo json_encode(array_values(array_filter(array_unique($results),'filterOnTerm')));
  return;
}

if($extension) {
  $regexExtension = '/\.(?:'.$extension.')$/';
} else {
  $regexExtension = '/\.(?:gabc|txt|ly)$/';
}

if(substr($root,0,1)=='[') {
  $roots = json_decode($root,true);
  $root = '';
}

$result = array();
foreach (ip_allFiles("autocompletes/$root.txt") as $i => $acFile) {
  if(file_exists($acFile)) {
    $remove = $_REQUEST['remove'];
    $results = file_get_contents($acFile);
    $results = preg_split('/\\r|\\n/',$results,NULL,PREG_SPLIT_NO_EMPTY);
    if($remove && ip_hasWriteAccess($acFile)) {
      $flip = array_flip($results);
      unset($results[$flip[$remove]]);
      $results = implode("\n",array_values($results));
      file_put_contents($acFile,$results);
    }
    $result = array_merge($result,array_values(array_filter($results,'filterOnTerm')));
  }
}
if(count($result)>0) {
  echo json_encode(array_unique($result));
  return;
}

$index = strrpos($term, '/');
if($index === false) {
  $termbeginning = '';
  if($roots) {
    echo json_encode(array_values(array_filter($roots,'filterOnTerm')));
    return;
  }
} else {
  $termbeginning = substr($term, 0, $index+1);
}

$dirs = ip_allFiles("$root$term");
$result = array();
foreach ($dirs as $i => $dir) {
  $index = strrpos($dir, '/');
  $beginning = substr($dir,$index + 1);
  $dir = substr($dir,0,$index);
  if(is_dir($dir)) {
    $entries = ip_scandir($dir);
    foreach($entries as $v) {
      if(substr($v, 0, strlen($beginning)) == $beginning) {
        if($extension=='/'){
          if(is_dir("$dir/$v") && !preg_match('/^\.|^\.{1,2}$/',$v)) {
            $result[] = $termbeginning . $v;
          }
        } else if(is_dir("$dir/$v") && !preg_match('/^\.|^\.{1,2}$/',$v)) {
          $result[] = $termbeginning . $v . '/';
        } else if(preg_match($regexExtension,$v)) {
          if($hideExtension) {
            $v = $preg_replace($regexExtension,'',$v);
          }
          $result[] = $termbeginning . $v;
        }
      }
    }
  }
}
echo json_encode(array_unique($result));
?>